using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using VariationSettings.Enums;

namespace VariationSettings
{
    /// <summary>
    /// This class is used to validate and procces the command arguments
    /// </summary>
    internal class ProgramInputs
    {
        private readonly bool argsValid = false;
        private readonly string url = string.Empty;
        private Operation currentOperation = Operation.Unknown;
        private readonly string fileName = string.Empty;
        private readonly bool createHierarchy = false;

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="args">The arguments from the console application</param>
        public ProgramInputs(string[] args)
        {
            if (args.Length == 0)
            {
                return;
            }

            switch (args[0])
            {
                case "-help":
                    return;
                case "-url":
                case "-import":
                case "-export":
                case "-filename":
                case "-create":
                    break;
                default:
                    Console.WriteLine("Argument '{0}' is invalid", args[0]);
                    PrintHelp();
                    return;
            }
    
            // loop through arguments
            for (int i = 0; i < args.Length; i++)
            {
                switch (args[i].ToLower())
                {
                    case "-url":
                        url = args[i + 1];
                        i++;
                        break;
                    case "-import":
                        if (!SetOperation(Operation.Import)) return;
                        break;
                    case "-export":
                        if (!SetOperation(Operation.Export)) return;
                        break;
                    case "-filename":
                        fileName = args[i + 1];
                        i++;
                        break;
                    case "-create":
                        createHierarchy = true;
                        break;
                    default:
                        Console.WriteLine("Argument '{0}' is invalid", args[i]);
                        break;
                }
            }

            // If we are here, everything executed well
            argsValid = true;
        }

        #region Properties

        /// <summary>
        /// The filename specified
        /// </summary>
        public string FileName
        {
            get { return fileName; }
        }

        /// <summary>
        /// The opeartion to perform
        /// </summary>
        public Operation CurrentOperation
        {
            get { return currentOperation; }
        }

        /// <summary>
        /// The url of the sitecollection
        /// </summary>
        public string Url
        {
            get { return url; }
        }

        /// <summary>
        /// Are arguments valid
        /// </summary>
        public bool ArgsValid
        {
            get { return argsValid; }
        }

        /// <summary>
        /// Should the variation hierarchy be created
        /// </summary>
        public bool CreateHierarchy
        {
            get { return createHierarchy; }
        }

        #endregion

        #region Helper functions

        /// <summary>
        /// Sets the currentOperation, checks if this is allready done.
        /// </summary>
        /// <param name="newOperation">the operation to set</param>
        /// <returns>if the operation is succesfully set</returns>
        private bool SetOperation(Operation operation)
        {
            if (currentOperation == Operation.Unknown)
            {
                currentOperation = operation;
                return true;
            }
            else
            {
                Console.WriteLine("The operation is specified twice.");
                return false;
            }
        }

        /// <summary>
        /// Prints the help to the output
        /// </summary>
        internal static void PrintHelp()
        {
            Console.Write(
     @"
=================================================
VariationSettings - Help
=================================================

Usage: VariationSettings -url <UrlToSite> -filename <FileName> -import [-create]
       VariationSettings -url <UrlToSite> -filename <FileName> -export

       or 

       VariationSettings -help

*********** Used For ***********
Export current variationsetting to an XML file.
Import variationsettings from an XML file to SharePoint.

*********** Required Parameters are: ***********
-url <UrlToSite>
-file <FileName.xml>

*********** Optional Parameters are: ***********
-create
");

        }

        #endregion

    }
}
